#ifndef _TGOBJECT_H
#define _TGOBJECT_H

#include <list>
#include <string>

#include "math/mat4x4.h"
#include "AABB.h"

namespace TG
{
	class Component;

	class TGObject
	{
	private:
		TGObject();

	public:
		vec3 localPosition;
		vec3 localRotation;
		vec3 scale;

		vec3 position;
		vec3 rotation;

		std::string name;
		AABB aabb;
		bool enable;

		TGObject* parent = NULL;
		std::list<TGObject*> childs;
		std::list<Component*> components;

		static TGObject* instantiate(const char* name);
		static void destroy(TGObject* go);

		virtual ~TGObject();

		void setEnable(bool b);

		mat4x4 getMatrix();

		void addChild(TGObject* go);
		void setParent(TGObject* parent);

		template<class T>
		bool hasComponent()
		{
			for (std::list<Component*>::iterator it = components.begin(); it != components.end(); ++it)
			{
				if (typeid(T) == typeid(**it))
				{
					return true;
				}
			}
			return false;
		}

		template<class T>
		T* getComponent()
		{
			for (std::list<Component*>::iterator it = components.begin(); it != components.end(); ++it)
			{
				if (typeid(T) == typeid(**it))
				{
					return *it;
				}
			}
			return NULL;
		}

		template<class T>
		T* addComponent()
		{
			T* t = new T();
			components.push_back(t);
			return t;
		}
	};
}
#endif